
#include "Application.h"
#include "sqlite.h"
#include <QMessageBox>
#include "MainWindow.h"
#include "common/common.h"
#include "common/actionmanager/actionmanager.h"
#include "common/actionmanager/actioncontainer.h"
#include "services/window/windowservice.h"

#include "base/abstractwidget.h"
#include "base/abstractaction.h"
#include "base/abstractmenu.h"



void Demo::initialize()
{
}

bool Demo::start()
{
    auto windowService = dpfGetService(dpfservice::WindowService);
    auto widget = new MainWindow;

    if (windowService) {
        QAction *action = new QAction("sqlite", this);
        auto sqliteWindow = new AbstractWidget(widget);
        windowService->registerWidget("sqlite", sqliteWindow);

        action->setIcon(QIcon(QString(":icons/database")));
        // Add navigation to navigationbar
        windowService->addNavigationItem(new AbstractAction(action), Priority::lowest);
        
        // activated when triggered navigation
        connect(action, &QAction::triggered, this, [=](){
            // show mainwindow
            windowService->showWidgetAtPosition("sqlite", dpfservice::Position::Central, true);
        }, Qt::DirectConnection);
    }

    return true;
}

dpf::Plugin::ShutdownFlag Demo::stop()
{
    return Sync;
}



static QString message = QString();

void db4sMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();

    // Emulate the default Qt treatment. This might not work in some OS, like Windows.
    fprintf(stderr, "%s\n", localMsg.constData());

    const char *file = context.file ? context.file : "";
    const char *function = context.function ? context.function : "";
    localMsg = QString("%1 (%2, %3:%4)\n").arg(msg, function, file).arg(context.line).toLocal8Bit();

    // Log using the SQLite log mechanism, so it gets the same treatment than messages
    // reported by SQLite itself. This will allow these messages to be seen in our Log window.
    // Error code will be the type
    sqlite3_log(static_cast<int>(type), localMsg.constData());
}

void boxMessageOutput(QtMsgType, const QMessageLogContext &, const QString &msg)
{
    message += msg + "\n";
}

